package com.orangelife.mobile.parkingcard.biz;

import android.os.Handler;

import com.android.volley.Request;
import com.curry.android.adapter.FatherAdapter;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.JSONHelper;
import com.google.gson.reflect.TypeToken;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.ParkingCardURL;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yyx
 * on 2015/11/10.
 */
public class ParkingCardHttpBiz {

    public static void delPakingCard(String parkingCardID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("{cardID}", parkingCardID);
        VolleyHttpUtil.getInstance().delWithCallback(ParkingCardURL.PARKCARD_DELETE, map, callback);
    }

    /** 请求私人已认证的车位 */
    public static void getHasCertParkingSpace(VolleyHttpUtil.ResponseCallBack callback) {
        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.HAS_CERT_PAKING_SPACE, callback);
    }

    public static void getHasCertParkingSpace1(Handler handler,int what) {
        VolleyHttpUtil.getInstance().getWithHandler(ParkingCardURL.HAS_CERT_PAKING_SPACE,handler,what);
    }

    public static void getParkingCardList(String pageSize, String pageNo, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        if (pageSize == null || pageNo == null) {
            map.put("pageSize", pageSize);
            map.put("pageIndex", pageNo);
        }
        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKCARD_LIST, map, callback);
    }

    /**
     * 请求列表
     *
     * @param map
     * @param callback
     */
//    public static void requesPost(HashMap<String, String> map, OrangeHttpUtil.ResponseCallBack callback, String url) {
//        OrangeHttpUtil.getInstance().sendRequestPostWithCallback(url, map, callback);
//    }
    public static void requesPost(HashMap<String, Object> map, VolleyHttpUtil.ResponseCallBack callback, String url) {
        VolleyHttpUtil.getInstance().postWithCallback(url, map, callback);
    }

    /**
     * 请求列表
     * @param callback
     */
//    public static void requesAdd(HashMap<String, String> map, OrangeHttpUtil.ResponseCallBack callback, String url) {
//        OrangeHttpUtil.getInstance().sendRequestWithCallback(url, Request.Method.POST,
//                map, null, callback, null, -1, null);
//    }

    public static void requestDelete(HashMap<String, String> map, VolleyHttpUtil.ResponseCallBack callback, String url) {
        VolleyHttpUtil.getInstance().delWithCallback(url, map, callback);
    }


    /**
     * 列表回调
     *
     * @param response
     * @param errCode
     * @return
     */
    public static List<Map<String, Object>> handleOrderList(JSONObject response, int errCode) {
        List<Map<String, Object>> list = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            list = OrangeDataHandler.getListFromJsonWithPageEntity(
                    response, new TypeToken<List<Map<String, Object>>>() {
                    }.getType(), null, null);

        }
        return list;
    }

    /**
     * 详情回调
     *
     * @param response
     * @param errCode
     * @return
     */
    public static Map<String, Object> handleOrderDetail(JSONObject response, int errCode) {
        Map<String, Object> bean = null;
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            Map<String, Object> map = JSONHelper
                    .jsonToMap(String.valueOf(response));
            bean = (Map<String, Object>) map.get("entity");
        }
        if (bean == null) {
            bean = JSONHelper
                    .jsonToMap(String.valueOf(response));
        }
        return bean;
    }

    /**
     * 更新商家信息列表
     *
     * @param list2
     */
    public static void updateListView(List<Map<String, Object>> list2, int flags, PullToRefreshListView lv,
                                      List<Map<String, Object>> list, FatherAdapter adapter) {
        if (list2 == null) {
            if (flags == 1) {
                list.clear();
                lv.setPullLoadEnable(false);
                adapter.setList(list);
                adapter.notifyDataSetChanged();
            } else {
                lv.setPullLoadEnable(false);
            }
        }
        if (list2 != null && list != null) {
            if (list2.size() == 0) {
                lv.setPullLoadEnable(false);
            } else {
                if (flags == 1) {
                    list.clear();
                    lv.setPullLoadEnable(true);
                    list.addAll(list2);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                } else {
                    lv.setPullLoadEnable(true);
                    list.addAll(list2);
                    adapter.setList(list);
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}
